function [] = bayes()


regex = {
%Ham-specific:
'\slet\sme\sknow',
'\splease\s',
'\sthanks\s',
'\ssubject\s',
%Spam-specific:
'http',
'\s[b8].?[uv].?[li17\|].?[gq].?[a@/][\\]?r.?[yi1!:\|]?',
'\sc.?[i1!:\|].?[a@/][\\]?.?[li17\|].?[i1!:\|].?[s235$]',
'\sd.?[i1!:\|].?[s235$].?c.?[oQ0].?[uv].?n.?[t\+]?',
'\s[LlIi17\|].?[Ee3].?[Vv\\][/]?.?[Ii1!:\|].?[Tt\+].?[Rr].?[Aa@/]?[\\]?',
'\sm.?[oQ0].?n.?[e3€].?y',
'\s[OoQ0].?[Mm].?[Ee3€].?[Ggq].?[Aa@/][\\]?',
'\sp+.?[e3€]+.?n+.?[i1!:\|]+.?[s5$]+\s',
'pharmaceuticals',
'prescription',
'\s[Rr].?[OoQ0].?[LlIi17\|].?[Ee3€].?[Xx]',
'\s[Ss25$].?[Ee3€].?[Xx].?[Yy]?\s',
'\s[Ss25$].?[OoQ0].?[Mm].?[Aa/][\\]?\s',
'[pP]+.?[i1!:\|]+.?[l1\|]+.?[s5$]\s',
'(?i)[vV\\]+/?.?[i:1!\|]+.?[a@/]+\\?.?[gq]+.?r+.?[a@/]+\\?',
'\s+[:Alpha]+\s+',
'\$'
};
%we got some good regexps from following sources
%http://wiki.spamihilator.com/doku.php?id=en:tutorials:regex

%the file listings
spamtrainfiles = dir('spam/train');
hamtrainfiles = dir('ham/train');
spamtestfiles = dir('spam/test');
hamtestfiles = dir('ham/test');

%get the feature arrays for ham and spam
strain = zeros(length(spamtrainfiles)-2,length(regex));
htrain = zeros(length(hamtrainfiles)-2,length(regex));
for i=3:length(spamtrainfiles),
   strain(i-2,:) = presentre(strcat('spam/train/', spamtrainfiles(i).name), regex);
end 
for i=3:length(hamtrainfiles),
   htrain(i-2,:) = presentre(strcat('ham/train/', hamtrainfiles(i).name), regex);
end

%calculate the class probabilities...
spamamount = length(spamtrainfiles)-2;
hamamount = length(hamtrainfiles)-2;
pCspam = spamamount/(spamamount + hamamount);
pCham = hamamount/(spamamount + hamamount);

%... and means
hammean = mean(htrain);
spammean = mean(strain);


%this is the threshold to move more spam mails in the direction of ham
theta = 0.005;

    %test the spam test set
    tp = 0;
    fp = 0;
    for i=3:length(spamtestfiles),
        stest = presentre(strcat('spam/test/', spamtestfiles(i).name), regex);
        
        %the prior
        spamprob = 1;
        hamprob = 1;
        for j=1:length(stest),
            spamprob = spamprob * spammean(j)^stest(j)*(1-spammean(j))^(1-stest(j));
            hamprob = hamprob * hammean(j)^stest(j)*(1-hammean(j))^(1-stest(j));
        end
        
        %the posterior
        pChamX = (hamprob * pCham) / pCspam;
        pCspamX = (spamprob * pCspam) / pCham;
        
        if((pCspamX - pChamX) > 0)
            if (abs(pCspamX - pChamX) < theta)
                fp = fp+1;
            else
                tp = tp+1;
            end
        else
                fp = fp+1;
        end
    end
    
    %test the ham test set
    tn = 0;
    fn = 0;
    for i=3:length(hamtestfiles),
        htest = presentre(strcat('ham/test/', hamtestfiles(i).name), regex);
        spamprob = 1;
        hamprob = 1;
        %the prior
        for j=1:length(stest),
            spamprob = spamprob * spammean(j)^htest(j)*(1-spammean(j))^(1-htest(j));
            hamprob = hamprob * hammean(j)^htest(j)*(1-hammean(j))^(1-htest(j));
        end
        
        %the posterior for comparison
        pChamX = (hamprob * pCham) / pCspam;
        pCspamX = (spamprob * pCspam) / pCham;
        
        if((pCspamX - pChamX) > 0)
            if (abs(pCspamX - pChamX) < theta)
                tn = tn+1;
            else
                fn = fn+1;
            end
        else
            tn = tn+1;
        end
    end

    %output
    sprintf('true positives:\t\t%d\nfalse positives:\t%d\ntrue negatives:\t\t%d\nfalse negatives:\t%d\ncorrectly classified:\t%.2f%\n', tp, fp, tn, fn, ((tn+tp) / (tp+tn+fp+fn))*100)    
    
end
